// pages/my_order/my_order.js
var common = require("../../utils/public.js") //引入public.js脚本
const api = require('../../utils/api.js')
const time = require('../../utils/time.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sLoad:false,
    x:0,
    currentTab: 0,
    list: [],
    index:0,
    size:100,
    time:[],
    newTime:true,
    current:1,//分页默认
    orderList:[
      {
        tabName:"全部",
        id:"0"
      },
      {
        tabName: "待付款",
        id: "1"
      },
      {
        tabName: "待配送",
        id: "2"
      },
      {
        tabName: "已发货",
        id: "3"
      },
      {
        tabName: "待评价",
        id: "5"
      },
      {
        tabName: "已完成",
        id: "6"
      },
      {
        tabName: "撤销订单",
        id: "7"
      },
      {
        tabName: "退换/售后",
        id: "8"

      },
    ],
    currentTab:0,
    type:0
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    this.setData({
      index: e.currentTarget.dataset.current,
      sLoad: false
    })
    this.tab()
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
       
      })
    }
  },
  // 跳转到订单详情
  order_details: common.throttleFunc(function (e) {
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?orderCode=' + e.currentTarget.dataset.id,
    })
  }),
  //跳转售后详情
  toDetail: common.throttleFunc(function (e) {
    wx.navigateTo({

      url: '/pages/ss_detail/ss_detail?id=' + e.currentTarget.dataset.id

    })
  }),
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let index = options.index
    this.setData({
      index: options.index,
      currentTab: options.index,
      type: options.type,
    })
    if (index==8){
      this.setData({
        x:448
      })
    }
  },


  tab: function () {
    let that = this
    let size = that.data.size
    var currentTab = ''
    if (that.data.index == 0) {
      currentTab = ''
    } else if (that.data.index == 1) {
      currentTab = 0
    } else if (that.data.index == 2) {
      currentTab = 1
    } else if (that.data.index == 3) {
      currentTab = 2
    } else if (that.data.index == 5) {
      currentTab = 4
    } else if (that.data.index == 6) {
      currentTab = 5
    } else if (that.data.index == 7) {
      currentTab = '6' + ',' + '7' + ',' + '8'
    } 
    if (that.data.index == 8) {
      that.setData({
        list:[]
      })
      api.selectExceptAll({
        createid: wx.getStorageSync("openId"),
         page: {
          startPage: false,//是否开启上滑加载
           size: size,//请求数据 渲染几条
          current: 1,//当前页
        }
      }).then(res=>{
        console.log(res)
        if (res.result){
          that.setData({
            list: res.object.records,
            sLoad: true,
            size
          })
        }
      })
    } else if (that.data.index == 1){
      api.getIPage({
        userInfoId: wx.getStorageSync("openId"),
        cityId: wx.getStorageSync("cityId"),
        page: {
          current: that.data.current,
          size: size
        },
        status: currentTab
      }).then(res => {
        if (res.result) {
          var arr = []
          that.setData({
            list: res.object.records
          })
          res.object.records.forEach(function (v, index) {
            arr.push(time.formatTimeTwo(v.createtime, 'Y/M/D h:m:s')),
              v.newTime = true
            if (v.payovertime > Date.parse(new Date())) {
              v.newTime = false
            } else {
              v.newTime = true
              api.orderRefund3({
                orderCode: v.orderCode,
                status: '8',
              }).then(res => {
                if (res.result) {
                } else if (res.code == 998) {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none'
                  })
                }
              })           
            }
          })

          that.setData({
            list: res.object.records,
            time: arr,
            sLoad: true,
            size
          })
        }
      })
    }else{
      api.getIPage({
        userInfoId: wx.getStorageSync("openId"),
        cityId: wx.getStorageSync("cityId"),
        page: {
          current: that.data.current,
          size: size
        },
        status: currentTab
      }).then(res => {
        if (res.result) {
          var arr = []
          that.setData({
            list: res.object.records
          })
          res.object.records.forEach(function (v,index) {
            arr.push(time.formatTimeTwo(v.createtime, 'Y/M/D h:m:s')),
              v.newTime = true
            if (v.payovertime > Date.parse(new Date())){
              v.newTime = false
              }else{
              v.newTime = true
              }
          })
          
          that.setData({
            list: res.object.records,
            time: arr,
            sLoad:true,
            size
          })
        }
      })
    }
   

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      sLoad: false
    })
    this.tab()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    if(this.data.type==3){
      wx.navigateBack({
        delta: 2
      })
    }
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let size = this.data.size
    size = size + 50
    this.tab()
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})